package cc.mrbird.febs.buss.service.impl;

import cc.mrbird.febs.buss.entity.DepartResultData;
import cc.mrbird.febs.buss.entity.ResultData;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.HallUpData;
import cc.mrbird.febs.buss.mapper.HallUpDataMapper;
import cc.mrbird.febs.buss.service.IHallUpDataService;
import cc.mrbird.febs.system.entity.Dept;
import cc.mrbird.febs.system.service.IDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;
import cc.mrbird.febs.common.utils.SortUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 *  Service实现
 *
 * @author Lujingyuan
 * @date 2020-04-24 14:01:42
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HallUpDataServiceImpl extends ServiceImpl<HallUpDataMapper, HallUpData> implements IHallUpDataService {

    /*@Autowired
    private HallUpDataMapper hallUpDataMapper;*/
    @Autowired
    private IDeptService deptService;

    @Override
    public IPage<HallUpData> findHallUpDataDetail(QueryRequest request, HashMap<String,String> map) {
        Page<HallUpData> page = new Page<HallUpData>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.baseMapper.findHallUpDataDetailPageByMap(page,map);
    }

    @Override
    public List<HallUpData> findHallUpDatas(HallUpData hallUpData) {
	    LambdaQueryWrapper<HallUpData> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createHallUpData(HallUpData hallUpData) {
        this.save(hallUpData);
    }

    @Override
    @Transactional
    public void updateHallUpData(HallUpData hallUpData) {
        this.saveOrUpdate(hallUpData);
    }

    @Override
    @Transactional
    public void deleteHallUpData(HallUpData hallUpData) {
        LambdaQueryWrapper<HallUpData> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    @Transactional
    public void deleteHallUpData(String[] ids) {
        List<String> list = Arrays.asList(ids);
        // 删除用户
        this.removeByIds(list);
    }

    @Override
    public List<HallUpData> findHallUpDataListByMap(Map map) {
        List<HallUpData> list=this.baseMapper.findHallUpDataListByMap(map);
        return list;
    }

    /**
     * 根据map 获取一个部门的结果展示数据
     * @param map
     * @return
     */
    @Override
    public List<HallUpData> getResultDataList(Map map) {
        List<HallUpData> list=findHallUpDataListByMap(map);
        HashMap<Long,HallUpData> datamap =new HashMap<Long,HallUpData>();
        Integer seekBussCount=0;
        Integer mailBussCount=0;
        for(HallUpData data:list){
            if(data.getItem()!=null&&"0".equals(data.getDataType())){
                //说明是事项数据
                if(datamap.get(data.getItem())==null){
                    datamap.put(data.getItem(),data);
                }else{
                    data.setOnlineBussCount(data.getOnlineBussCount()+datamap.get(data.getItem()).getOnlineBussCount());
                    data.setUnderlineHall(data.getUnderlineHall()+datamap.get(data.getItem()).getUnderlineHall());
                    data.setUnderlineSubHall(data.getUnderlineSubHall()+datamap.get(data.getItem()).getUnderlineSubHall());
                    datamap.put(data.getItem(),data);
                }
            }else{
                seekBussCount+=data.getSeekBussCount();
                mailBussCount+=data.getMailBussCount();
            }
        }
        List<HallUpData> resultList=new ArrayList<HallUpData>();
        for (HashMap.Entry<Long,HallUpData> entry : datamap.entrySet()) {
            entry.getValue().setSeekBussCount(seekBussCount);
            entry.getValue().setMailBussCount(mailBussCount);
            resultList.add(entry.getValue());
        }
        return resultList;
    }

    /**
     * 获取多个部门的结果展示数据
     * @param map
     * @return
     */
    @Override
    public List<DepartResultData> getDepartResultDataList(Map map) {
        List<DepartResultData> departResultDataList=new ArrayList<DepartResultData>();
        if(map.get("departId")!=null&&map.get("departId").toString().trim().length()>0){
            List<HallUpData> list=this.getResultDataList(map);
            if(list!=null&&list.size()>0){
                DepartResultData drd=new DepartResultData();
                drd.setDepartName(list.get(0).getDepartName());
                drd.setDataList(list);
                drd.setMailBussCount(list.get(0).getMailBussCount());
                drd.setSeekBussCount(list.get(0).getSeekBussCount());
                departResultDataList.add(drd);
            }
        }else{
            List<Dept> deptlist=deptService.list();
            for(Dept dept:deptlist){
                map.put("departId",dept.getDeptId()+"");
                List<HallUpData> list=this.getResultDataList(map);
                if(list!=null&&list.size()>0){
                    DepartResultData drd=new DepartResultData();
                    drd.setDepartName(dept.getDeptName());
                    drd.setDataList(list);
                    drd.setMailBussCount(list.get(0).getMailBussCount());
                    drd.setSeekBussCount(list.get(0).getSeekBussCount());
                    departResultDataList.add(drd);
                }
            }
        }

        return departResultDataList;
    }


    @Override
    public void deleteDataByTime(String bussTime, Long bid,String dataType) {
        LambdaQueryWrapper<HallUpData> queryWrapper = new LambdaQueryWrapper<>();
        if("0".equals(dataType)){
            queryWrapper.eq(HallUpData::getItem,bid);
        }else{
            queryWrapper.eq(HallUpData::getDepart,bid);
        }
        queryWrapper.eq(HallUpData::getDataType,dataType);
        queryWrapper.eq(HallUpData::getIsDel,0);
        queryWrapper.between(HallUpData::getBussTime,bussTime+" 00:00:00",bussTime+" 23:59:59");
        List<HallUpData> list=this.baseMapper.selectList(queryWrapper);
        for(HallUpData d:list){
            d.setIsDel(1);
            this.saveOrUpdate(d);
        }
    }

    @Override
    public List<HallUpData> getUpData(String bussTime, Long bid,String dataType){
        LambdaQueryWrapper<HallUpData> queryWrapper = new LambdaQueryWrapper<>();
        if("0".equals(dataType)){
            queryWrapper.eq(HallUpData::getItem,bid);
        }else{
            queryWrapper.eq(HallUpData::getDepart,bid);
        }
        queryWrapper.eq(HallUpData::getDataType,dataType);
        queryWrapper.eq(HallUpData::getIsDel,0);
        queryWrapper.between(HallUpData::getBussTime,bussTime+" 00:00:00",bussTime+" 23:59:59");
        List<HallUpData> list=this.baseMapper.selectList(queryWrapper);
        return list;
    }

}
